﻿using System;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using HelpersDotNet.Serialization;

namespace HelpersDotNet.Extensions
{
    public static partial class Extensions
    {
        public static T ToEnum<T>(this string value, bool ignoreCase)
            where T : struct
        {
            if (string.IsNullOrEmpty(value))
                throw new ArgumentNullException("Can't parse empty string.");

            Type enumType = typeof(T);

            if (!enumType.IsEnum)
                throw new InvalidOperationException("The type is not of enumeration.");

            return (T)Enum.Parse(enumType, value, ignoreCase);
        }

        /// <summary>
        /// Ignores case by default
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T ToEnum<T>(this string value)
            where T : struct
        {
            return value.ToEnum<T>(true);
        }
    }
}